#!coding=utf-8
from standard_function import *

def del_appdown_task(driver):
    #清空下载任务
    try:
        btn=WaitforElementById(driver,'com.pp.assistant:id/e5',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        btn.click()

    try:
        WaitforElementById(driver,'com.pp.assistant:id/ch',30).click()
        WaitforElementById(driver,'com.pp.assistant:id/b50',30).click()
        WaitforElementById(driver,'com.pp.assistant:id/dd',30).click()
        WaitforElementById(driver,'com.pp.assistant:id/vi',30).click()
    except NoSuchElementException:
        pass
    driver.back()

def clickappdown(driver):
    try:
        btns=WaitforElementsById(driver,"com.pp.assistant:id/yt",40)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        for btn in btns:
            try:
                size=WaitforElementById(btn,"com.pp.assistant:id/l_",30).get_attribute("text")
            except NoSuchElementException:
                continue
            print size
            if 'G' not in size:
                if 'K' in size or float(size[0:-2])<10:
                    continue
            btn.click()
            try:
                ss=WaitforElementById(driver,'com.pp.assistant:id/fp',5)
                down_btn=WaitforElementById(ss,'com.pp.assistant:id/aa4',5)
            except NoSuchElementException:
                driver.back()
                continue
            else:
                down_btn.click()

            try:
                delet=WaitforElementById(driver,'com.pp.assistant:id/ab5',5)
            except NoSuchElementException:
                pass
            else:
                delet.click()

            waittime=20
            while waittime:
                try:
                    text=WaitforElementByClass(ss,'android.widget.TextView',5).get_attribute("text")
                except NoSuchElementException:
                    down_btn.click()
                    break
                print text
                if '0.0' not in text:
                    down_btn.click()
                    break
                else:
                    waittime=waittime-1
                    time.sleep(0.5)
                    if waittime==0:
                        down_btn.click()
                        break
            driver.back()




def soft(driver):
    categorys = [u'影音播放',u'系统工具',u'通讯社交',u'手机美化',u'新闻阅读',u'摄影图像',u'考试学习',u'网上购物', \
                 u'金融理财',u'生活休闲',u'旅游出行',u'健康运动',u'办公商务',u'育儿亲子',u'休闲益智',u'跑酷竞速',u'体育竞技', \
                 u'扑克棋牌',u'动作冒险',u'网络游戏',u'经营策略',u'飞行射击',u'角色扮演',u'辅助工具']

    try:
        WaitforElementByName(driver,u'分类',30).click()
    except NoSuchElementException:
        raise NoSuchElementException
    print random.randint(0,8)
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    for i in categorys:
        print "Current categorys is:",i
        try:
            WaitforElementByName(driver,i,5).click()
        except NoSuchElementException:
            continue

        try:
            WaitforElementByName(driver,u'全部',30).click()
        except NoSuchElementException:
            driver.back()
            continue
        else:
            try:
                clickappdown(driver)
            except NoSuchElementException:
                raise NoSuchElementException
            else:
                driver.back()

def run():
    desired_caps = {}
    #desired_caps['automationName'] = 'uiautomator2'
    desired_caps['platformName'] = "Android"               # 声明是ios还是Android系统
    desired_caps['platformVersion'] = ANDROID_VERSION              # Android内核版本号，也可以在模拟器设置中查看
    desired_caps['deviceName'] = DEVICE         # 连接的设备名称，可通过adb devices查看
    desired_caps['appPackage'] = 'com.pp.assistant'    # apk的包名
    desired_caps['appActivity'] = '.activity.MainActivity'          # apk的launcherActivity
    desired_caps['noReset']='true'                         #不需要再次安装apk
    driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)          # 建立 session
    time.sleep(3)
    del_appdown_task(driver)
    soft(driver)
    driver.quit()

#run()